from rest_framework import status
from rest_framework.response import Response
from rest_framework.generics import RetrieveAPIView, CreateAPIView

from api.serializer.account import LoginRegisterModelSerializer


class LoginRegisterAPIView(RetrieveAPIView, CreateAPIView):
    """登录注册视图"""
    # post请求进行
    authentication_classes = []

    def post(self, request, *args, **kwargs):
        ser = LoginRegisterModelSerializer(data=request.data)

        if ser.is_valid():
            # 获取数据返回
            result = {
                "status": 1000,
                "data": {
                    "user_id": ser.context.get('user_id'),
                    "username": ser.context.get('username'),
                    "phone": ser.context.get('phone'),
                    "avatar": ser.context.get('avatar'),
                    "bucket": ser.context.get('bucket'),
                    "region": ser.context.get('region'),
                    "balance": ser.context.get('balance'),
                    "fans": ser.context.get('fans'),
                    "follow": ser.context.get('follow'),
                    "token": ser.context.get('token'),
                }
            }
            return Response(data=result, status=status.HTTP_200_OK)
        error = ser.errors
        return Response(data=error, status=status.HTTP_400_BAD_REQUEST)
